<template>
  <div  id="index">
    <el-container>
      <el-dialog title="修改姓名" :visible.sync="dialogFormVisible" style="position: absolute;width: 50%;
    left: 25pc;">
        <el-form>
          <el-form-item label="姓名">
            <el-input v-model="username" autocomplete="off" style="width: 22pc;
    float: left;"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary"  @click="updateName">确 定</el-button>
        </div>
      </el-dialog>
      <el-header>
        <el-row>
          <el-col :span="6" style="font-size: 28px;">购物货比平台</el-col>
          <el-col :span="16" style="text-align: right;
                                    padding-right: 22px;"
                      v-if="this.$store.state.username != ''">
            欢迎您&nbsp;&nbsp;{{this.$store.state.username}}</el-col>
            <el-col :span="16" style="text-align: right;
                                    padding-right: 22px;"
                      v-else>
            未登录，请<router-link to="/">登录</router-link></el-col>
          <el-col :span="2">
            <el-dropdown>
              <span class="el-dropdown-link">
                <i class="el-icon-s-tools"></i>
              </span>
              <el-dropdown-menu slot="dropdown" v-if="this.$store.state.username != ''">
                <el-dropdown-item class="clearfix" >
                  <p @click="dialogFormVisible = true">修改名字</p>
                </el-dropdown-item>
                <el-dropdown-item class="clearfix"  >
                  
                  <p @click="exit">退出</p>
                </el-dropdown-item>
              </el-dropdown-menu>
              <!-- <el-dropdown-menu slot="dropdown">
                <button type="text" style="border: none; background: none;" @click="dialogFormVisible = true"><el-dropdown-item>修改名字</el-dropdown-item></button>
                <button type="text" style="border: none; background: none;" @click="exit"><el-dropdown-item>退出</el-dropdown-item></button>
              </el-dropdown-menu> -->
            </el-dropdown>
          </el-col>
        </el-row>
      </el-header>
      <el-container style="height: 41.3pc">
        <el-aside width="200px">
          <el-row class="tac">
            <el-col :span="24">
              <h5>导航条</h5>
              <el-menu
                default-active="2"
                class="el-menu-vertical-demo"
                @open="handleOpen"
                @close="handleClose">
                <el-submenu index="1">
                  <template slot="title">
                    <i class="el-icon-location"></i>
                    <span>商家</span>
                  </template>
                  <el-menu-item-group>
                    <template slot="title">阿里</template>
                    <el-menu-item index="1-1"><router-link :to="'/business/' + '淘宝'">淘宝</router-link></el-menu-item>
                    <el-menu-item index="1-2"><router-link :to="'/business/' + '天猫'">天猫</router-link></el-menu-item>
                  </el-menu-item-group>
                  <el-menu-item-group title="京东">
                    <el-menu-item index="1-3"><router-link :to="'/business/' + '京东'">京东</router-link></el-menu-item>
                  </el-menu-item-group>
                  <el-submenu index="1-4">
                    <template slot="title">其他</template>
                    <el-menu-item index="1-4-1"><router-link :to="'/business/' + '苏宁易购'">苏宁易购</router-link></el-menu-item>
                  </el-submenu>
                </el-submenu>
                <el-menu-item index="2">
                  <i class="el-icon-menu"></i>
                  <span slot="title"><router-link to="/goods">商品</router-link></span>
                </el-menu-item>
                <el-menu-item index="3" disabled>
                  <i class="el-icon-document"></i>
                  <span slot="title">我的钱包</span>
                </el-menu-item>
                <el-menu-item index="4">
                  <i class="el-icon-setting"></i>
                  <span slot="title">购物车</span>
                </el-menu-item>
              </el-menu>
            </el-col>
          </el-row>
        </el-aside>
        <el-main>
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name:'',
  data () {
    return {
        dialogFormVisible: false,
        username: ''
    }
  },
  created() {
    this.util.post('/user/test').then(res => {
      console.log(res);
    })
    if (localStorage.getItem('username') != null) {
      this.$store.state.username =  localStorage.getItem('username')
    }
  },
  methods: {
    handleOpen(key, keyPath) {
      console.log(key, keyPath);
    },
    handleClose(key, keyPath) {
      console.log(key, keyPath);
    },
    updateName () {
      let formdata = new FormData();
      formdata.append('oldUsername', this.$store.state.username);
      formdata.append('newUsername', this.username);
      this.dialogFormVisible = false
      axios({
        method: 'post',
        url: 'http://localhost:8000/public/updateNameByName',
        data: formdata
      }).then(res => {
        localStorage.setItem('username', this.username)
        this.$store.state.username =  this.username
        this.username = ''
      })
    },
    exit () {
      this.util.post('/logout').then(res => {
        if (res.data == 'loginPage')
          this.$router.push('/')
        this.$store.state.username = ''
         localStorage.setItem('username', '')
      })
    }
  },

}

</script>
<style lang='' scoped>
/* #index {
  background-image: url(../assets/index.jpg);
  background-size: 100%;
  background-repeat: no-repeat;
  background-position: center center;
} */
.el-header, .el-footer {
    background-color: #B3C0D1;
    color: #333;
    text-align: left;
    height: 6pc;
    line-height: 3.5pc;
    padding-left: 114px;
  }
  
  .el-aside {
    background-color: #D3DCE6;
    color: #333;
    text-align: center;
  }
  
  .el-main {
    /*background-color: #E9EEF3;*/
    color: #333;
    text-align: center;
  }
  
  body > .el-container {
    margin-bottom: 40px;
  }
  .el-dropdown-link {
    cursor: pointer;
    color: #409EFF;
  }
  .el-icon-arrow-down {
    font-size: 12px;
  }
</style>